<?php

session_start();
require_once "lib/all.php";
load_globals();
ActiveRecord::$database_settings = parse_ini_file(Cabbie::$approot . "/config/database.ini", true);

if (empty(Cabbie::$root) || empty(Cabbie::$approot))
    die("Cabbie could not determine where it is placed! Edit .htaccess please.");

/* include application helpers */
if (is_dir(Cabbie::$approot . "/helpers")) {
    $d = dir(Cabbie::$approot . "/helpers");
    while (false !== ($entry = $d->read())) {
        /* include php files, they're supposed to be helpers */
        if (is_file(Cabbie::$approot . "/helpers/$entry") &&
                substr($entry, - 4) == ".php")
            include_once(Cabbie::$approot . "/helpers/$entry");
    }
}

function load_globals()
{
    if (PATH_SEPARATOR == ";") {
        Cabbie::$temp_path = "C:\\windows\\temp\\";
        Cabbie::$slash = "\\";
    } else {
        Cabbie::$temp_path = "/tmp/";
        Cabbie::$slash = "/";
    }
/*    $include_path = explode(PATH_SEPARATOR, ini_get("include_path"));

    // determine where cabbie is placed
    foreach($include_path as $path) {
        if (file_exists("$path/lib/cabbie.php"))
            Cabbie::$root = $path;
        else if (is_dir("$path/controllers"))
            Cabbie::$approot = $path;
    }
*/
    /* include routing tables */
    if (file_exists(Cabbie::$approot . "/config/routes.php"))
        include(Cabbie::$approot . "/config/routes.php");

    /* set global uri params */
    $uri = explode("/", $_SERVER["REQUEST_URI"]);
    array_splice($uri, 0, 3);
    Cabbie::$uri_params = $uri;
}

function file_exists_incpath ($file)
{
    $paths = explode(PATH_SEPARATOR, get_include_path());

    foreach ($paths as $path) {
        // Formulate the absolute path
        $fullpath = $path . DIRECTORY_SEPARATOR . $file;
        // Check it
        if (file_exists($fullpath)) {
            return $fullpath;
        }
    }

    return false;
}

function __autoload($class_name)
{
    if (empty(Cabbie::$root) || empty(Cabbie::$approot))
        load_globals();
	if (strstr($class_name, "Controller")) {
		$key="controller_$class_name";
		if(Cabbie::$class_cache === true) {
		$classdata=apc_fetch($key);
		} else $classdata=false;
		if($classdata === false) {

		$class_name = str_replace("Controller", "_controller.php", $class_name);
        $class_name = strtolower($class_name);
        if (file_exists(Cabbie::$approot . "/controllers/$class_name"))
            $classdata=file_get_contents(Cabbie::$approot . "/controllers/$class_name");
			if(Cabbie::$class_cache === true) apc_store($key, $classdata, Cabbie::$class_cache_ttl);
			//require_once "controllers/$class_name";
		}
		eval("?>" . $classdata . "<?");
    }else {
        if (file_exists_incpath($class_name . '.class.inc.php')) {
            include_once($class_name . '.class.inc.php');
        } else {
            if (substr($class_name, 0, 1) == "_") {
                $dirname = substr($class_name, 1, strpos($class_name, "_", 2) - 1) . "/";
            } else $dirname = "";
            if (file_exists(Cabbie::$approot . "/models/$dirname" . $class_name . ".php"))
                require_once "models/$dirname" . $class_name . ".php";
			else {
				$key="classes_$class_name";
				$classdata=apc_fetch($key);
				if($classdata === false) {
						$key="classes_$class_name";
					if(Cabbie::$class_cache === true) {
						$classdata=apc_fetch($key);
						} else $classdata = false;
						if($classdata === false) {
							if (file_exists(Cabbie::$approot . "/classes/" . strtolower($class_name) . ".php"))
							{
								$classdata=file_get_contents(Cabbie::$approot . "/classes/" . strtolower($class_name) . ".php");
								if(Cabbie::$class_cache === true) apc_store($key, $classdata, Cabbie::$class_cache_ttl);
								eval("?>" . $classdata . "<?");
    						}
					} else {
						eval("?>" . $classdata . "<?");
					}
						//require_once "classes/" . strtolower($class_name) . ".php";
            	} else if (method_exists('Cabbie', 'autoload'))
					Cabbie::autoload_cabbie($class_name);
				}
        }
    }
}

?>